package org.ucas.faker.web.service.Visualization.json;

import java.text.DecimalFormat;
import java.util.*;
import java.util.regex.Pattern;

public class DataUtil {

    /*
  * 判断是否为整数
  * @param str 传入的字符串
  * @return 是整数返回true,否则返回false
    */
    public static boolean isInteger(String string) {
        if (string == null){
            return false;
        }
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(string).matches();
    }

    public static <T> boolean isIntegerList(List<T> list){
        if (list == null) {
            return true;
        }
        for (T t : list){
            if (!isInteger(t.toString())){
                return false;
            }
        }
        return true;
    }

    public static <K, V> Map<K, V> sortMapByKey(Map<K, V> map, boolean ascend){
        List<Map.Entry<K, V>> list = new ArrayList<Map.Entry<K, V>>(map.entrySet());

        Collections.sort(list,new Comparator<Map.Entry<K, V>>() {
            public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
                int scale = 1;
                if (ascend){
                    scale = -1;
                }
                return o2.getKey().toString().compareTo(o1.getKey().toString())*scale;
            }

        });
        Map<K, V> resultMap = new TreeMap<K, V>();
        for (Map.Entry<K, V> entry : list){
            resultMap.put(entry.getKey(), entry.getValue());
        }
        return resultMap;
    }

    public static <T> List<Double> getPercentageData(List<T> list){
        double error = 1e-4;
        double sum = 0.0;
        for (T t : list){
            sum += Double.parseDouble(t.toString());
        }

        DecimalFormat df = new DecimalFormat("0.00");
        double currentSum = 0.0;
        List<Double> resultList = new ArrayList<Double>();
        for (int i=0;i<list.size()-1;i++){
            double value = 0.0;
            if (Math.abs(sum)>error){
                value = Double.parseDouble(df.format(Double.parseDouble(list.get(i).toString())/sum));
            }
            resultList.add(value);
            currentSum += value;
        }
        if (Math.abs(sum)>error) {
            resultList.add(Math.max(0.0, Double.parseDouble(df.format(1.0 - currentSum))));
        } else{
            resultList.add(0.0);
        }
        double testSum = 0.0;
        for (double d : resultList){
            testSum += d;
        }

        if (testSum-1.0>error){
            double diff = testSum - 1.0;
            for (int k=0;k<resultList.size();k++){
                if (resultList.get(k)>diff){
                    resultList.set(k, Double.parseDouble(df.format(resultList.get(k)-diff)));
                    break;
                }
            }
        }

        testSum = 0.0;
        for (double num : resultList){
            testSum += num;
        }

//        System.out.println("sum===="+testSum);

        if (testSum<=1.0){
            return resultList;
        }

        double diff = testSum - 1.0;
        for (int k=0;k<resultList.size();k++){
            if (resultList.get(k)>diff){
                resultList.set(k, Double.parseDouble(df.format(resultList.get(k)-diff)));
                break;
            }
        }

        return resultList;
    }

    public static <T> double aveLength(List<T> list){
        int length = 0;
        if (list == null){
            return 0.0;
        }
        if (list.size() == 0){
            return 0.0;
        }
        for (T element : list){
            length += element.toString().length();
        }
        return (length+0.0)/(list.size()+0.0);
    }
}
